package com.ycw.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.ycw.dao.HouseDao;
import com.ycw.pojo.House;
import com.ycw.pojo.pageIndex;
import com.ycw.util.DBUtil;
import com.ycw.util.MyBatisUtil;

public class HouseDaoImpl extends DBUtil implements HouseDao {
	MyBatisUtil myBatisUtil = new MyBatisUtil();
	SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
	SqlSession sqlSession = sqlSessionFactory.openSession();

	/*
	 * 查询数据总数===========================
	 * 
	 * @see com.ycw.dao.HouseDao#getHouseSum()
	 */
	@Override
	public int getHouseSum() {
		int sum = sqlSession.getMapper(HouseDao.class).getHouseSum();
		return sum;
	}

	/*
	 * 分页查询数据===========================
	 * 
	 * @see com.ycw.dao.HouseDao#getAllHouse(com.ycw.pojo.pageIndex)
	 */
	@Override
	public List<House> getAllHouse(pageIndex pa) {
		int sum = getHouseSum();
		List<House> houses = sqlSession.getMapper(HouseDao.class).getAllHouse(pa);
		System.out.println("sum的值：" + sum);
		houses.get(0).setSum(sum);
		sqlSession.close();
		return houses;
	}

	/*
	 * 通过id查询数据===========================
	 * 
	 * @see com.ycw.dao.HouseDao#getHouseDetail(com.ycw.pojo.House)
	 */
	@Override
	public House getHouseDetail(House house) {

		House house2 = sqlSession.getMapper(HouseDao.class).getHouseDetail(house);

		return house2;
	}

	/*
	 * 添加数据
	 * 
	 * @see com.ycw.dao.HouseDao#addHouseInfo(com.ycw.pojo.House)
	 */
	@Override
	public int addHouseInfo(House house) {
		System.out.println("添加：" + house.toString());
		int i = 0;
		try {
			i = sqlSession.getMapper(HouseDao.class).addHouseInfo(house);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
		return 0;

	}

	/*
	 * 通过id修改数据
	 * 
	 * @see com.ycw.dao.HouseDao#updateHouseInfo(com.ycw.pojo.House)
	 */
	@Override
	public int updateHouseInfo(House house) {
		System.out.println("修改之前：" + house.toString());
		int i = 0;
		try {
			i = sqlSession.getMapper(HouseDao.class).updateHouseInfo(house);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}

		return i;
	}

	@Override
	public int deleteHouseInfo(House house) {

		int i = 0;
		try {
			i = sqlSession.getMapper(HouseDao.class).deleteHouseInfo(house);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}

		return i;

	}

	@Override
	public int getSumByCondition(House house) {
		int i = sqlSession.getMapper(HouseDao.class).getSumByCondition(house);
		return i;

	}

	@Override // 根据地区，有无双气，价格区间查询
	public List<House> getHouseByCondition(House house) {
		System.out.println("输出：" + house.toString());
		int sum = getSumByCondition(house);
		System.out.println("sum:" + sum);
		List<House> houses = sqlSession.getMapper(HouseDao.class).getHouseByCondition(house);

		houses.get(0).setSum(sum);
		return houses;

	}

}
